// 任务队列
const queue: any[] = []
// 控制创建Promise的开关,让Promise只创建一次
let isFlushPending = false;


export function nextTick(fn) {
    return fn ? Promise.resolve().then(fn) : Promise.resolve();
}

export function queueJobs(job) {
    if (!queue.includes(job)) {
        queue.push(job);
    }
    queueFlush();
}

function flushJobs() {
    let job;
    isFlushPending = false;
    while (job = queue.shift()) {
        job && job()
    }
}

function queueFlush() {
    if (isFlushPending) return;
    isFlushPending = true;
    nextTick(flushJobs)
}
